package com.azt.provider.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.azt.api.pojo.EntrustReq;
import com.azt.api.pojo.SmsParam;
import com.azt.api.service.MqProducerService;
import com.azt.api.service.MsgConfigService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azt.api.pojo.EntrustApply;
import com.azt.api.service.EntrustApplyService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.EntrustApplyMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class EntrustApplyServiceImpl implements EntrustApplyService {

	@Autowired
	private EntrustApplyMapper entrustApplyMapper;

	@Autowired
	private MsgConfigService msgConfigService;

	@Override
	public Pagination getEntrustApplyPage(int pageno, int pagesize, Map<String, String> searchMap) {

		Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		PageHelper.orderBy("state asc, createtime desc");
		List<EntrustApply> list = entrustApplyMapper.getEntrustApplyByParams(searchMap);
		Pagination p = new Pagination(startPage, list);
		p.setList(list);
		p.setParams(searchMap);
		return p;

	}

	@Override
	public void updateEntrustApply(EntrustApply apply) {
		entrustApplyMapper.updateByPrimaryKey(apply);
	}

	@Override
	public void savePurchaseApply(EntrustReq req) {
		EntrustApply apply = new EntrustApply();
		apply.setApplyType(2);
		apply.setState(0);
		if (StringUtils.isNotBlank(req.getBudget())) {
			apply.setBudget(Double.valueOf(req.getBudget()));
		}
		BeanUtils.copyProperties(req, apply);
		entrustApplyMapper.insert(apply);
		// 发送短信通知运营
		Map<String, String> params = new HashMap<>();
		params.put("companyName", apply.getCompanyName());
		params.put("contactName", apply.getContactName());
		params.put("contactTel", apply.getContactTel());
		msgConfigService.sendMessage("ENTRUST_APPLY", params);
	}

	@Override
	public EntrustApply getEntrustApplyByid(Integer id) {
		return entrustApplyMapper.selectByPrimaryKey(id);
	}

	@Override
	public void deleteEntrustApplyById(Integer id) {
		entrustApplyMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void updateMemo(Integer id, String memo) {
		entrustApplyMapper.updateMemo(id, memo);
	}
}
